package com.atguigu.yeb.server.controller.admin;


import com.atguigu.yeb.server.base.R;
import com.atguigu.yeb.server.pojo.entity.TAdmin;
import com.atguigu.yeb.server.pojo.entity.TAdminLoginParam;
import com.atguigu.yeb.server.service.TAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huafeng
 * @since 2021-12-04
 */
@RestController
@Api(tags = "控制用户登陆验证的类")
@RequestMapping("/admin")
public class TLoginController {
    @Autowired
    private TAdminService tAdminService;

    @ApiOperation(value = "登陆之后返回token")
    @PostMapping("/login")
    public R login(@RequestBody TAdminLoginParam loginParam, HttpServletRequest request){
        return tAdminService.login(loginParam.getUsername(),loginParam.getPassword(),request);
    }

    @ApiOperation(value = "获取当前用户信息")
    @GetMapping("user/userInfo")
    public R getUserInfo(Principal principal){
        String name = principal.getName();
        TAdmin admin = tAdminService.getUserByName(name);
        admin.setPassword(null);
        return R.ok().data("UserInfo",admin);
    }
    @ApiOperation(value = "注销")
    @PostMapping("logOut")
    public R getUserInfo(){
        return R.ok().message("注销成功");
    }

    @ApiOperation(value = "测试")
    @PostMapping("/hello/test")
    public R Test(){
        return R.ok().message("测试连接成功");
    }

}

